import { createBrowserRouter, Navigate } from "react-router-dom";
import React, { Suspense, useContext } from "react";
import { AuthContext } from "../LTC/utils/AuthContext";
const Login = React.lazy(() => import("../LTC/pages/Login"));
const Main = React.lazy(() => import("../LTC/pages/Main"));
const Ai = React.lazy(() => import("../ZJX/Ai"));
const Createuser = React.lazy(() => import("../LTC/pages/Createuser"));
const CarDetails = React.lazy(() => import("../LTC/pages/CarDetails"));
const Zi  = React.lazy(()=>import("../JJK/zi"))
const Shi  = React.lazy(()=>import("../JJK/shi"))
const Tubiao = React.lazy(() => import("../JJX/Tubiao"))
const Demo = React.lazy(() => import("../JJX/Demo"))
// 修正导入路径大小写
const Shang = React.lazy(()=>import("../JJK/shang.jsx"));
//路由守卫
// const ISlogin  =(props)=>{
//     let Com  = props.children.type
//     if(sessionStorage.getItem("token")){
//         return <Com></Com>
//     }else{
//         return <Navigate to="/login"></Navigate>
//     }
// }

function RequireAuth({ children }) {
  const { accessToken } = useContext(AuthContext);
  if (!accessToken) return <Navigate to="/login" />;
  return children;
}

const router = createBrowserRouter([
  { path: "/login", element: <Login /> },
  { path: "/createuser", element: <Createuser /> },
  { path: "/", element: <Navigate to="login" /> },
  {
    path: "/main",
    element: (
      <RequireAuth>
        <Main />
      </RequireAuth>
    ),
  },
  {
    path: "/ai",
    element: (
      <RequireAuth>
        {" "}
        <Ai />{" "}
      </RequireAuth>
    ),
  },
  {
    path: "/car-details",
    element: (
      <RequireAuth>
        <CarDetails />
      </RequireAuth>
    ),
  },
     {path:"/zi",element:<Zi/>},
    {path:"/detail/:id",element:<Shi/>},
    {path:"/shang/:id?",element:<Shang/>}, // 确保配置了可选id参数
    {path: "/tubiao", element:(<RequireAuth><Tubiao /></RequireAuth>)},
    {path: "/demo", element: <Demo/>}
]);
export default router;
